<?php

using('Views.IView');
using('Library.XSL');
using('Library.DataTypes.Array.Conversion');

class View_HTML extends IView
{
    var $_Error;
    var $_Model;
    var $_Output;
    var $_Profiler;

    var $_Theme;
    var $_Template;
    var $_Filters;

    function __construct()
    {
        Vault::Add('View', &$this);

        parent::__construct();

        $this->_Model    = &Vault::Get('Model');
        $this->_Error    = &Vault::Get('Debug_Error');
        $this->_Profiler = &Vault::Get('Debug_Profiling');
    }

    function Render()
    {
        $this->_Error->Log('View[HTML] :: Starting Page Generation', W_LOG_DEBUG);
        $this->_Profiler->Start('Render Page');
        
        /*
         * Attempt to load the specified template file and theme
         */
        
        $this->_Theme    = $this->_Model->Get('Theme');
        $this->_Template = $this->_Model->Get('Template');

        $this->_Profiler->Start('Load Theme');
        if (!is_dir($themepath = W_ENV_PATH.'/Themes/'.(($this->_Theme)?$this->_Theme:'Default')))
            $this->_Error->Log('View[HTML] :: Theme \''.str_replace(W_ENV_PATH, '', $themepath).'\' Doesnt Exist', W_LOG_CRIT);
        else
            $this->_Error->Log('View[HTML] :: Theme \''.str_replace(W_ENV_PATH, '', $themepath).'\' Loaded.');

        if (!is_file($tplfile = $themepath.'/'.(($this->_Template)?$this->_Template:'default').'.tpl'))
            $this->_Error->Log('View[HTML] :: Theme Template \''.str_replace(W_ENV_PATH, '', $tplfile).'\' Doesnt Exist.', W_LOG_CRIT);

        $template = file_get_contents($tplfile);
        $this->_Profiler->Stop();
      
        /*
         * Process the theme configuration file.
         */
        
        if (!file_exists($conf = ($themepath.'/config.inc.php')))
            $this->_Error->Log('View[HTML] :: Unable to locate the configuration file \''.$conf.'\'.', W_LOG_CRIT);
        else
        {
            include $conf;
            
            $this->_Filters = $settings['Filters'];
            
            for ($i = 0, $imax = count($settings['CSS']); $i < $imax; $i++)
                $this->_Model->Set('Page.Header.CSS', array($settings['CSS'][$i]));
                
            for ($i = 0, $imax = count($settings['JS']); $i < $imax; $i++)
                $this->_Model->Set('Page.Header.JS', array($settings['JS'][$i]));            
        }
        
        /*
         * We now have a complete template file, pass the template content
         * through each filter.
         */

        $this->_Profiler->Start('Generate list of filters to be applied');
        $dir   = W_ENV_PATH.'/Views/HTML/Filters/';
		$files = array();
		$dh    = opendir($dir);

		while ($file = readdir($dh))
			if (in_array(substr($file, 3, -4), $this->_Filters))
				$files[] = $file;

		closedir($dh);
		sort($files);
		$this->_Profiler->Stop();

		foreach ($files as $file)
		{
			include_once $dir.$file;

			if (class_exists($class = 'View_HTML_'.substr($file, 3, strlen($file) - 7)))
			{
			    $this->_Profiler->Start('Apply Filter \''.$class.'\'');
			    $filter   = new $class();
			    $template = $filter->Process($template);
			    $this->_Profiler->Stop();
			}
			else
				$this->_Error->Log('View[HTML] :: Filter \''.$filter.'\' Not Found.', W_LOG_CRIT);
		}

		$this->_Output = $template;
		$this->_Profiler->Stop();
				
    }

    function Output()
    {
        echo $this->_Output;
    }

    function ThemeFile($file)
    {
        if ($file[0] == '/')
            return $file;
        else
        {
            if (file_exists($f = W_ENV_PATH.'/Themes/'.$this->_Theme.'/'.$file))
                return '/Themes/'.$this->_Theme.'/'.$file;
            else
            {
                if (file_exists($f = W_ENV_PATH.'/Themes/Default/'.$file))
                    return '/Themes/Default/'.$file;
                else
                    return false;
            }
        }
    }
}

?>